/*
	libxbn is a small and tiny xml binary node
	Copyright (C) 2010  Thomas Kraus (thomentor@gmx.de)

	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.

	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	 License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this library; if not, write to the Free Software Foundation,
	Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
#include "xpath_plugins/xbn_xpath_plugins_1_0.h"
#include "xbn_helper.h"
#include "xbstring.h"
#include "xbn_document.h"

xpathplugin_concat::xpathplugin_concat()
{
	min_parameter = 2;
	max_parameter = -1;
	name          = "concat";
}
bool            xpathplugin_concat::testUnit()
{
	return	testPass( "concat('Ha','llo')", 1, xbnode::xbn_temp, "Hallo" )
		&	testPass( "concat('Ha','ll','o')", 1, xbnode::xbn_temp, "Hallo" );
}
bool            xpathplugin_concat::operator() (xbn_state &state, xbn_xpath *pxpath )
{
	if( !state.pCurrentNode )
		return false;

	xbn_state arg1(state);
	if( !pxpath->process( arg1, *(state.pCurrent_token) ) )
		return false;

	if( arg1.ResultNodeList.size()!=1 )
	{
		pxpath->raiseError( name+"() resultsizes arg1 does not match",&state, __FILE__, __LINE__ );
		return false;
	}

	std::string text = arg1.ResultNodeList.front()->getText();
	while( pxpath->peekFunctionList( *(state.pCurrent_token) ) )
	{
		xbn_state argn(state);
		if( !pxpath->process( argn, *(state.pCurrent_token) ) )
			return false;

		if( argn.ResultNodeList.size()!=1 )
		{
			pxpath->raiseError( name+"() resultsizes argn does not match",&state, __FILE__, __LINE__ );
			return false;
		}
		text+=argn.ResultNodeList.front()->getText();
	}
	state.ResultNodeList.push_back(
			state.pStack->createTempNode(
				text
				)
			);
	return true;
}

